package com.cdtu.room.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.cdtu.room.domain.Equipment;
import com.cdtu.room.dto.RoomDto;
import com.cdtu.room.mapper.EquipmentMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cdtu.room.mapper.RoomMapper;
import com.cdtu.room.domain.Room;
import com.cdtu.room.service.IRoomService;

/**
 * 会议室Service业务层处理
 * 
 * @author hcc
 * @date 2024-02-27
 */
@Service
public class RoomServiceImpl implements IRoomService 
{
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;
    /**
     * 查询会议室
     * 
     * @param roomId 会议室主键
     * @return 会议室
     */
    @Override
    public Room selectRoomByRoomId(Long roomId)
    {
        return roomMapper.selectRoomByRoomId(roomId);
    }

    /**
     * 查询会议室列表
     * 
     * @param room 会议室
     * @return 会议室
     */
    @Override
    public List<Room> selectRoomList(Room room)
    {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增会议室
     * 
     * @param room 会议室
     * @return 结果
     */
    @Override
    public int insertRoom(Room room)
    {
        room.setDelFlag("0");
        room.setStatus(0);
        room.setCreateBy(SecurityUtils.getUsername());
        room.setCreateTime(DateUtils.getNowDate());
        return roomMapper.insertRoom(room);
    }

    /**
     * 修改会议室
     * 
     * @param room 会议室
     * @return 结果
     */
    @Override
    public int updateRoom(Room room)
    {
        room.setUpdateBy(SecurityUtils.getUsername());
        room.setUpdateTime(DateUtils.getNowDate());
        return roomMapper.updateRoom(room);
    }

    /**
     * 批量删除会议室
     * 
     * @param roomIds 需要删除的会议室主键
     * @return 结果
     */
    @Override
    public int deleteRoomByRoomIds(Long[] roomIds)
    {
        return roomMapper.deleteRoomByRoomIds(roomIds);
    }

    /**
     * 删除会议室信息
     * 
     * @param roomId 会议室主键
     * @return 结果
     */
    @Override
    public int deleteRoomByRoomId(Long roomId)
    {
        return roomMapper.deleteRoomByRoomId(roomId);
    }

    /**
     * 获取会议室设备名称
     * @param roomId
     * @return
     */
    @Override
    public RoomDto getEquipmentNameByRoomId(Long roomId) {
        List<Equipment> equipmentList = equipmentMapper.getEquipmentNameByRoomId(roomId);
        RoomDto roomDto = new RoomDto();
        Room room = roomMapper.selectRoomByRoomId(roomId);
        BeanUtils.copyProperties(room,roomDto);
        // 使用流操作将equipmentList中的所有equipmentName值拼接成一个字符串，以逗号分隔
        String equipmentName = equipmentList.stream()
                .map(Equipment::getEquipmentName)
                .collect(Collectors.joining(", "));

        // 将拼接好的equipmentNames设置到roomDto的equipmentName字段中
        roomDto.setEquipmentName(equipmentName);
        return roomDto;
    }
}
